package com.dacd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dacd.pdf.R;

import java.io.File;

public class PdfFileAdapter extends RecyclerView.Adapter<PdfFileAdapter.ViewHolder> {

    private File[] listFile;

    public PdfFileAdapter(File[] listFile) {
        this.listFile = listFile;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(viewGroup.getContext(), R.layout.item_pdf_file, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        String fileName = listFile[i].getName();
        viewHolder.fileNameTv.setText(fileName);
        viewHolder.fileNameTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(v, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listFile == null ? 0 :listFile.length;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView fileNameTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fileNameTv = (TextView) itemView.findViewById(R.id.ipf_file_name_tv);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
